<?php
namespace Home\Controller;
use Think\Controller;
class CheckoutController extends Controller {
	public function index(){

		if(isset($_SESSION['username'])){
        	$this->assign('login','true');
        	$this->assign('username',$_SESSION['username']);
        }

        //当前登录用户的id
        $userid=$_SESSION['id'];

        //购物车的数量
        $cart = D('cart');
        $count = $cart->where("userid='$userid' ")->count();
        $this->assign('count',$count);

        //购物车所有的产品
        $cartArr = $cart ->where("userid='$userid' ")->select();
        $this->assign('cartArr',$cartArr);

         //获取总金额
        $total = 0;
        foreach ($cartArr as $item) {
            $total += $item['discountprice'] * $item['number'];
        }
        $this->assign('total',$total);


        $this->display();
        

	}

     //退出
    public function doLogout(){
        $_SESSION = array();
        if(isset($_COOKIE[session_name()])){
            setcookie(session_name(),'',time()-1,'/');
        }
        session_destroy();
        $this->redirect('Index/index');
    }

    //删除购物车
    public function delete(){
        $phoneid = $_POST['phoneid'];
        $cart = D('cart');
        $success = $cart->where(" phoneid='$phoneid' ")->delete();
        if($success){
            $result = array(
                'code'=> '0',
                'ext'=> 'success'
            );
            echo json_encode($result);
        }else {
            $result = array(
                'code'=> '1',
                'ext'=> 'fail'
            );
            echo json_encode($result);
        }
    }

    //结算
    public function pay(){
        $relprice = $_POST['relprice'];
        $user_id = $_SESSION['id'];
        $cartphone = D('cart');
        $cartidArr = $cartphone->field('phoneid')->where("userid='$user_id' ")->order('id desc')->select();
        $cartid = array();
        for ($i=0; $i < count($cartidArr); $i++) { 
              $cartid[$i] = $cartidArr[$i]['phoneid'];
        }
        $orderitemlist = implode(',', $cartid);
        $data['number'] = date('Ymd').str_pad(mt_rand(1,99999),5,'0',STR_PAD_LEFT); //自动生成订单号
        $data['user_id'] = $user_id;
        $data['orderitemlist'] = $orderitemlist;
        $data['create_at'] = date('Y-m-d H:i:s');
        $data['price'] = $relprice;

        $order = D('order');
        if($order->create($data)){
            $id= $order->add();
            if($id){
                $result = array(
                    'code' => '0',
                    'ext' => 'success',
                );
                echo json_encode($result);
                $cartphoneArr = $cartphone->where("userid='$user_id' ")->order('id desc')->select();
                $orderitem = D('orderitem');
                foreach ($cartphoneArr as $item) {
                    $data['phone_id'] = $item['phoneid'];
                    $data['count'] = $item['number'];
                    $data['price'] = $item['price'];
                    $data['discountprice'] = $item['discountprice'];
                    $orderitem->create($data);
                    $orderitem->add();
                }
                $cartphone->where("userid='$user_id'")->delete();    
            }
        }
        }

}